//: holding/ListIteration.java
package holding;
import typeinfo.pets.*;
import java.util.*;

/**
 * ListIterator只针对List实现
 * 相对通用的Iterator而言，他可以向前、向后移动以及获得前后元素的索引值
 * 通过set值甚至可以修改List的元素值
 */
public class ListIteration {
  public static void main(String[] args) {
    //这是由Pet生成器生成的Pet列表
    List<Pet> pets = Pets.arrayList(8);
    ListIterator<Pet> it = pets.listIterator();

    //向后遍历
    while(it.hasNext())
      System.out.print(it.next() + ", " + it.nextIndex() +
        ", " + it.previousIndex() + "; ");
    System.out.println();

    // Backwards:向前遍历
    while(it.hasPrevious())
      System.out.print(it.previous().id() + " ");
    System.out.println();
    System.out.println(pets);

    //从索引==3的位置开始遍历
    it = pets.listIterator(3);

    //向后遍历并且修改List的元素值
    while(it.hasNext()) {
      it.next();
      it.set(Pets.randomPet());
    }
    System.out.println(pets);
  }
} /* Output:输出
Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug, 5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7;
7 6 5 4 3 2 1 0//从后向前遍历索引值
[Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx]//修改前列表值
[Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster, EgyptianMau]//修改后列表值
*///:~
